<template>
	<mescroll-body style="
	white-space: nowrap;
	overflow-x: scroll;" ref="mescrollRef" @init="mescrollInit" @down="downCallback"
	 :up="upOption" @up="upCallback">
		<wTable ref="mychild" :columns="columns"  :data1="data" border @on-row-click="clickrow" @on-select-all="selectAll"
		 @on-select="selectO">
		</wTable>
  <mask-model ref="showMask" btnType="1" :titleContext="titleContext" @confirm="confirm" @cancel="cancel" :titleColoe="color" :cancelColor="color" :confirmColor="color" :maskTitle="maskTitle"></mask-model>
	<loading
		ref="loading"
		:custom="false"
		:shadeClick="true"
		:type="2"
		@callback="callback()">
		<!-- <view class="test">自定义</view> -->
	</loading>
	</mescroll-body>
</template>
<script>
	import maskModel from '../../components/mask-model/mask-model.vue'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import wTable from '../../components/wang-wTable/wTable.vue';
	import loading from '../../components/xuan-loading/xuan-loading.vue'
	export default {
		components: {
			wTable,
			maskModel,
			loading
		},
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		data() {
			return {
				color:this.$kdup.color,
				titleContext:"",
				maskTitle:"",
				SelectList: [],
				parentObj: {},
				columns: [],
				upOption: {
					auto: false, //是否在初始化后,自动执行上拉回调callback; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 50 // 每页数据的数量
					},
					noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
					empty: {
						tip: '~ 搜索无结果 ~' // 提示
					}
				},
				data: [], // 数据列表
				goods: [],
				curWord: "", //当前搜索关键词
				fieldType:"",
				
			}
		},
		onLoad(e) {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",	//文字颜色
				backgroundColor: this.$kdup.color	//底部背景色
			})
			this.index = e.index;
			this.fieldType=e.fieldType;
			var data = {
				key: e.fformkeyid,
				pageSize: "1",
				orgId: e.orgId,
				filter: e.ffilter,
				lookValue: "",
				fieldType: e.fieldType,
				stockId:e.stockId,
				auxKey:e.auxKey,
				sourcetypeId:e.sourcetypeId,
				bcdId:e.barid,
				number:e.number,
				countscheme:this.$kdup.countscheme,//盘点方案编码
				materialFnumber:e.materialFnumber
			};
			this.parentObj = data;
			this.$myRequest.postRequest("GetBaseDataList", this.parentObj).then(res => {
				this.open();
				if (res.data.error == "") {
					//找到数据
					var array = this.$kdup.split(res.data.Table[res.data.Table.length - 1].ffieldorder, ";");
					// 枚举值是13开启复选
					var headArray = this.$kdup.getTableHead(array, data.fieldType == 13 || data.fieldType == 57);
					var valueArray = this.$kdup.getTableContent(headArray, res.data.Table);
					this.columns = headArray;
					this.goods = valueArray;
					setTimeout(()=>{
						this.close();
					},800)
				} else {
					setTimeout(()=>{
						this.close();
					},500)
					console.log(res.data.error)
					this.titleContext="表格查询提示"
					this.maskTitle= res.data.error;
					this.$refs.showMask.show();
				}
			});
		},
		mounted() {

		},
		methods: {
			close:function(){
				this.$refs.loading.close();
			},
			open:function(){
				this.$refs.loading.open();
			},
			callback(){
			
			},
			cancel:function(){//取消按钮
			 uni.showToast({
			 	title: '已取消',  
			 	mask: false,  
			 	duration: 1000,  
			 	icon:"none"  
			 });  
			},
			confirm:function(){//确认按钮
			 uni.showToast({
			 	title: '已确认',  
			 	mask: false,  
			 	duration: 1000,  
			 	icon:"none"  
			 });  
			},
			clickrow({
				row
			}) {
				const pages = getCurrentPages(); //当前页
				const beforePage = pages[pages.length - 2]; //上个页面
				//如果选择了源单明细里面的源单则不需要放回重新调用enter
				this.$delete(row,'_isChecked')
				if(beforePage.route=="pages/source/source"){
					var data={
						val:row.fnumber,
						index: this.index,
						rowvalue:row
					}
				beforePage.$vm.inputVal1(JSON.stringify(data))
				}else{
				//放回调用enter从新赋值
					beforePage.$vm.submitAct(this.index, row)
				}
				uni.navigateBack({
					delta: 1
				});	
			},
			selectAll(allChecked) {
				this.SelectList = allChecked;
			},
			selectO({
				allChecked,
				checked
			}) {
				this.SelectList = allChecked;
			},
			onNavigationBarButtonTap(e) {
				const pages = getCurrentPages(); //当前页
				const beforePage = pages[pages.length - 2]; //上个页面
				var str = "";
				var obj=[];
				if (this.SelectList.allChecked != undefined) {
					str = this.SelectList.allChecked.map(function(elem, index) {
						obj.push(elem);
						return elem.number+"-"+elem.seq;
					}).join(",");
					
				} else {
					str = this.SelectList.map(function(elem, index) {
						obj.push(elem);
						return elem.number+"-"+elem.seq;
					}).join(",");
				}
				beforePage.$vm.submitAct(this.index, str,obj)
				uni.navigateBack({
					delta: 1
				});

			},
			onNavigationBarSearchInputConfirmed(e) {
				this.curWord = e.text // 已使用v-model,无需再次赋值
				// 节流,避免输入过快多次请求
				this.searchTimer && clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.doSearch(this.curWord)
				}, 300)
			},
			onNavigationBarSearchInputChanged(e) {
				this.curWord = e.text // 已使用v-model,无需再次赋值
				// 节流,避免输入过快多次请求
				this.searchTimer && clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					//this.doSearch(this.curWord)
				}, 300)
			},
			// 输入监听
			inputWord(e) {
				// this.curWord = e.detail.value // 已使用v-model,无需再次赋值
				// 节流,避免输入过快多次请求
				this.searchTimer && clearTimeout(this.searchTimer);
				this.searchTimer = setTimeout(() => {
					this.doSearch(this.curWord)
				}, 300)
			},
			// 搜索
			doSearch(word) {
				this.open();
				this.curWord = word
				this.data = []; // 先清空列表,显示加载进度
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				this.apiSearch(page.num, page.size, this.curWord).then(curPageData => {
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					this.mescroll.endSuccess(curPageData.length);
					//如果是第一页需手动制空列表
					if (page.num == 1) this.data = [];
					//追加新数据
					if(this.fieldType=='13' && this.data.length==0){
						this.data = this.data.concat(curPageData);
					}else if(this.fieldType!='13'){
						this.data = this.data.concat(curPageData);
					}
					this.$refs.mychild.isfalse(this.data);
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			}, //这里负责分页延迟显示
			apiSearch(pageNum, pageSize, keyword) {
				return new Promise((resolute, reject) => {
					//延时一秒,模拟联网
					setTimeout(() => {
						try {
							this.parentObj.pageSize = pageNum;
							this.parentObj.lookValue = keyword;
							this.$myRequest.postRequest("GetBaseDataList", this.parentObj).then(res => {
								if (res.data.error == "") {
									//找到数据
									var array = this.$kdup.split(res.data.Table[res.data.Table.length - 1].ffieldorder, ";");
									var headArray = this.$kdup.getTableHead(array, true);
									var valueArray = this.$kdup.getTableContent(headArray, res.data.Table);
									resolute(valueArray);
                                    setTimeout(()=>{
                                    	this.close();
                                    },500)
								} else {
									setTimeout(()=>{
										this.close();
									},500)
								console.log(res.data.error)
								this.titleContext="加载表格提示"
								this.maskTitle= res.data.error;
								this.$refs.showMask.show();
									resolute([]);
								}
							});
						} catch (e) {
							reject(e);
						}
					}, 1000)
				})
			}
		}
	}
</script>

<style lang="scss">
  page{background-color:#FFFFFF;}
	/*关键词搜索*/
	.item {
		padding: 20rpx;
	}

	.tip {
		font-size: 30rpx;
		vertical-align: middle;
	}

	.hot-word {
		font-size: 24rpx;
		margin-left: 30rpx;
		padding: 6rpx 40rpx;
		border: 2rpx solid #FF6990;
		border-radius: 100rpx;
		vertical-align: middle;
		color: #FF6990;
	}

	.word-input {
		display: inline-block;
		width: 60%;
		height: 50rpx;
		line-height: 50rpx;
		font-size: 24rpx;
		margin-left: 30rpx;
		border: 2rpx solid #18B4FE;
		border-radius: 60rpx;
		text-align: center;
		background-color: #fff;
		vertical-align: middle;
	}
</style>
